package com.zlm.UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author 13026
 */
public class UDP_receive_demo {
    /*
     * UDP数据接收方
     * */
    public static void main(String[] args) throws IOException {
        // 创建快递公司
        // 接收的时候一定要绑定端口
        // 端口要和发送的端口保持一致
        DatagramSocket ds = new DatagramSocket(10086);
        // 接收数据包
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        ds.receive(dp);
        // 解析数据包
        byte[] data = dp.getData();
        int length = dp.getLength();
        InetAddress address = dp.getAddress();
        int port = dp.getPort();
        System.out.println("接收到数据:" + new String(data));
        System.out.println("length:" + length);
        System.out.println("address:" + address);
        System.out.println("port:" + port);
    }
}
